public class MyQueue {

    public class ListNode{
        public int val;
        public ListNode prev;
        public ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    public void offer(int val){
        ListNode cur = new ListNode(val);
        if(head == null){
            head = cur;
            last = cur;
        }

        last.next = cur;
        cur.prev = last;
        last = cur;
        return;
    }

    //弹出元素
    public int roll(){
        // 没有元素
        try{
            checkQueueRollIsOK();
        }catch(QueueIsEmpty e){
            e.printStackTrace();
        }

        int record = head.val;
        //只有 一个元素
        if(head.next == null){
            head = null;
            last = null;
        }else{
            head = head.next;
            head.prev = null;
        }

        return record;

    }

    private void checkQueueRollIsOK()throws QueueIsEmpty{
        if(head == null){
            throw new QueueIsEmpty("Queue队列为空");
        }

    }

    public int peek(){
        // 没有元素
        try{
            checkQueueRollIsOK();
        }catch(QueueIsEmpty e){
            e.printStackTrace();
        }

        int record = head.val;
        return record;
    }


    public boolean isEmpty(){
        return head == null;
    }
}
